package com.mentalhealthplatform.schoolV1.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.mentalhealthplatform.appModels.Student;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Date;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

@Getter
@Setter
@NoArgsConstructor
public class StudentDTO extends UserDTO {
    private String address;
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", timezone = "UTC")
    private Date birthDate;
    private String name;
    private String number;
    private String gender;
    private String major;
    private String grade;
    private String college;

    // 全参构造方法
    public StudentDTO(UUID id, String username, String phone, boolean status, String address, Date birthDate, String name,
                      String number, String gender, String major, String grade, String college, Set<RoleSimpleDTO> roles) {
        super(id, username, phone, status, roles);
        this.address = address;
        this.birthDate = birthDate;
        this.name = name;
        this.number = number;
        this.gender = gender;
        this.major = major;
        this.grade = grade;
        this.college = college;
    }

    // 实体到 DTO 的转换方法
    public static StudentDTO convertToDTO(Student student) {
        Set<RoleSimpleDTO> roleDTOs = student.getRoles().stream()
                .map(role -> new RoleSimpleDTO(role.getId(), role.getIdentifier()))
                .collect(Collectors.toSet());


        return new StudentDTO(
                student.getId(),
                student.getUsername(),
                student.getPhone(),
                student.isStatus(),
                student.getAddress(),
                student.getBirthDate(),
                student.getName(),
                student.getNumber(),
                student.getGender(),
                student.getMajor(),
                student.getGrade(),
                student.getCollege(),
                roleDTOs
        );
    }

    // 将 StudentDTO 转换为 Student 实体对象
    public Student convertToEntity() {
        Student student = new Student();
        student.setId(this.getId());
        student.setUsername(this.getUsername());
        student.setPhone(this.getPhone());
        student.setStatus(this.isStatus());
        student.setAddress(this.getAddress());
        student.setBirthDate(this.getBirthDate());
        student.setName(this.getName());
        student.setNumber(this.getNumber());
        student.setGender(this.getGender());
        student.setMajor(this.getMajor());
        student.setGrade(this.getGrade());
        student.setCollege(this.getCollege());

        return student;
    }
}
